<template>
    <div class="app-container">
        <el-card class="selsctCard">
            <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
                <el-form-item label="工作流程key:" prop="key">
                    <el-input v-model="queryParams.key" placeholder="请输入工作流程key" clearable size="small"
                        @keyup.enter.native="handleQuery" />
                </el-form-item>
                <el-form-item label="流程名称:" prop="name">
                    <el-input v-model="queryParams.name" placeholder="请输入流程名称" clearable size="small"
                        @keyup.enter.native="handleQuery" />
                </el-form-item>
                <el-form-item label="状态:" prop="type">
                    <el-select v-model="queryParams.type" placeholder="请选择状态">
                        <el-option label="挂起" value="1"> </el-option>
                        <el-option label="激活" value="2"> </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item style="float: right">
                    <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                    <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>
        <div class="card-wrap card-padding">
            <el-table v-loading="loading" fit :data="definitionList" border>
                <el-table-column label="操作" width="120" align="center">
                    <template slot-scope="scope">
                        <el-button @click="handleUpdateSuspensionState(scope.row, 2)" type="text" size="small"
                            v-if="!scope.row.isSuspended" v-hasPermi="['definition:index:hangUp']">挂起</el-button>
                        <el-button @click="handleUpdateSuspensionState(scope.row, 1)" type="text" size="small"
                            v-if="scope.row.isSuspended" v-hasPermi="['definition:index:enable']">启用</el-button>
                        <el-button v-if="scope.row.modelId" @click="handleReadImage(scope.row)" type="text" size="small"
                            v-hasPermi="['definition:index:view']">查看</el-button>
                    </template>
                </el-table-column>
                <el-table-column label="工作流程key" align="center" prop="key" :show-overflow-tooltip="true" />
                <el-table-column label="流程名称" align="center" :show-overflow-tooltip="true">
                    <template slot-scope="scope">
                        <el-button type="text" @click="handleReadImage(scope.row)">
                            <span>{{ scope.row.name }}</span>
                        </el-button>
                    </template>
                </el-table-column>
                <el-table-column label="状态" align="center">
                    <template slot-scope="scope">
                        <el-tag v-if="!scope.row.isSuspended">激活</el-tag>
                        <el-tag type="success" v-if="scope.row.isSuspended">挂起</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="流程版本" align="center">
                    <template slot-scope="scope">
                        <el-tag size="medium">v{{ scope.row.version }}</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="更新人" align="center" prop="updateByName" />
                <el-table-column label="更新时间" align="center" prop="deployTime" />
            </el-table>

            <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize" @pagination="getList" />
        </div>
    </div>
</template>

<script>
import { getProcdefListList, updateState } from "@/api/flowable/model";

export default {
    name: "Definition",
    dicts: ["sys_process_category"],
    components: {},
    data() {
        return {
            // 遮罩层
            loading: false,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 流程定义表格数据
            definitionList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                name: null,
                category: null,
                key: null,
                tenantId: null,
                deployTime: null,
                derivedFrom: null,
                derivedFromRoot: null,
                parentDeploymentId: null,
                engineVersion: null,
            },
            formQueryParams: {
                pageNum: 1,
                pageSize: 10,
            },
            // 挂载表单到流程实例
            formDeployParam: {
                formId: null,
                deployId: null,
            },
            deployId: "",
            // xml
            flowData: {},
        };
    },
    created() {
        this.getList();
    },
    methods: {
        /** 查询流程定义列表 */
        getList() {
            this.loading = true;
            getProcdefListList(this.queryParams).then((response) => {
                if (response.code == 200) {
                    this.loading = false;
                    this.definitionList = response.rows;
                    this.total = response.total;
                }
            });
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },

        /** 流程图查看 */
        handleReadImage(info) {
            this.$tab
                .closePage({
                    path: "/flowable/definition/model",
                    name: "Model",
                })
                .then(() => {
                    // 执行结束的逻辑
                    this.$nextTick(() => {
                        this.$router.push({
                            path: "/flowable/definition/model",
                            query: {
                                modelId: info.modelId,
                                name: info.name,
                                category: info.category,
                                deployId: info.deploymentId,
                                state: "readonly",
                            },
                        });
                    });
                });
        },

        /** 挂起/激活流程 */
        handleUpdateSuspensionState(row, type) {
            const params = {
                id: row.id,
                state: type,
            };
            updateState(params).then((res) => {
                this.$modal.msgSuccess(res.msg);
                this.getList();
            });
        },
    },
};
</script>
<style scoped lang="scss">
.selsctCard {
    margin-bottom: 20px;
    border-radius: 8px;
}
</style>
